<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 攀登者1（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>攀登者喜欢寻找各种地图&#xff0c;并且尝试攀登到最高的山峰。</p> 
<p>地图表示为一维数组&#xff0c;数组的索引代表水平位置&#xff0c;数组的元素代表相对海拔高度。其中数组元素0代表地面。</p> 
<p>例如&#xff1a;[0,1,2,4,3,1,0,0,1,2,3,1,2,1,0]&#xff0c;代表如下图所示的地图&#xff0c;地图中有两个山脉位置分别为 1,2,3,4,5 和 8,9,10,11,12,13&#xff0c;最高峰高度分别为 4,3。最高峰位置分别为3,10。</p> 
<p>一个山脉可能有多座山峰(高度大于相邻位置的高度&#xff0c;或在地图边界且高度大于相邻的高度)。</p> 
<p><img alt="" height="239" src="https://img-blog.csdnimg.cn/63287f530c7641628550cf45ab259f28.png" width="594" /></p> 
<p>登山者想要知道一张地图中有多少座山峰。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入为一个整型数组&#xff0c;数组长度大于1。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出地图中山峰的数量。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:550px;"><tbody><tr><td>输入</td><td>0,1,4,3,1,0,0,1,2,3,1,2,1,0</td></tr><tr><td>输出</td><td>3</td></tr><tr><td>说明</td><td>山峰所在索引分别为3&#xff0c;10&#xff0c;12</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题考试时为核心代码模式&#xff0c;非ACM模式&#xff0c;即无需自己解析输入数据。</p> 
<p>本题代码实现仍然以ACM模式处理&#xff0c;但是会将输入处理 与 算法逻辑 分开&#xff0c;大家只看算法逻辑即可。</p> 
<p>本题逻辑较为简单&#xff0c;可以直接看代码实现。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

// 输入处理
void (async function () {
  const heights &#61; (await readline()).split(&#34;,&#34;).map(Number);
  console.log(getResult(heights));
})();

// 算法实现&#xff08;本题实际考试为核心代码模式&#xff0c;因此考试时只需要写出此函数实现即可&#xff09;
function getResult(heights) {
  let count &#61; 0;

  for (let i &#61; 0; i &lt; heights.length; i&#43;&#43;) {
    const leftHeight &#61; i - 1 &gt;&#61; 0 ? heights[i - 1] : 0;
    const rightHeight &#61; i &#43; 1 &lt; heights.length ? heights[i &#43; 1] : 0;

    if (heights[i] &gt; leftHeight &amp;&amp; heights[i] &gt; rightHeight) {
      count&#43;&#43;;
    }
  }

  return count;
}
</code></pre> 
<p> </p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  // 输入处理
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] heights &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();

    System.out.println(getResult(heights));
  }

  // 算法实现&#xff08;本题实际考试为核心代码模式&#xff0c;因此考试时只需要写出此函数实现即可&#xff09;
  public static int getResult(int[] heights) {
    int count &#61; 0;

    for (int i &#61; 0; i &lt; heights.length; i&#43;&#43;) {
      int leftHeight &#61; i - 1 &gt;&#61; 0 ? heights[i - 1] : 0;
      int rightHeight &#61; i &#43; 1 &lt; heights.length ? heights[i &#43; 1] : 0;

      if (heights[i] &gt; leftHeight &amp;&amp; heights[i] &gt; rightHeight) {
        count&#43;&#43;;
      }
    }

    return count;
  }
}
</code></pre> 
<p> </p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
heights &#61; list(map(int, input().split(&#34;,&#34;)))


# 算法入口&#xff08;本题实际考试为核心代码模式&#xff0c;因此考试时只需要写出此函数实现即可&#xff09;
def getResult(h):
    count &#61; 0

    for i in range(len(h)):
        leftH &#61; h[i - 1] if i - 1 &gt;&#61; 0 else 0
        rightH &#61; h[i &#43; 1] if i &#43; 1 &lt; len(h) else 0

        if h[i] &gt; leftH and h[i] &gt; rightH:
            count &#43;&#61; 1

    return count


# 算法调用
print(getResult(heights))

</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 100000

// 算法实现&#xff08;本题实际考试为核心代码模式&#xff0c;因此考试时只需要写出此函数实现即可&#xff09;
int getResult(const int heights[], int heights_size) {
    int count &#61; 0;

    for (int i &#61; 0; i &lt; heights_size; i&#43;&#43;) {
        int leftHeight &#61; i - 1 &gt;&#61; 0 ? heights[i - 1] : 0;
        int rightHeight &#61; i &#43; 1 &lt; heights_size ? heights[i &#43; 1] : 0;

        if (heights[i] &gt; leftHeight &amp;&amp; heights[i] &gt; rightHeight) {
            count&#43;&#43;;
        }
    }

    return count;
}

// 输入处理
int main() {
    int heights[MAX_SIZE];
    int heights_size &#61; 0;

    while (scanf(&#34;%d&#34;, &amp;heights[heights_size&#43;&#43;])) {
        if (getchar() !&#61; &#39;,&#39;) break;
    }

    printf(&#34;%d\n&#34;, getResult(heights, heights_size));

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>